<?php
include_once('class/autoload.php');

 

//récupération des informations du formulaire de connexion de la page base
if(isset($_POST ['connexion']))
{
	
	
	connecte();
	//header ( 'Location: http://localhost/projetusep/index.php' );
}
else
{
	if(isset($_POST ['deconnexion']))
	{
		deconnecte();
	
	}
	
	
}

function connecte() {
	
	$pdo = new connexpdo();
	
	
	if (isset ( $_POST ['connexion'] )) {
		if (isset ( $_POST ['login'] ) && isset ( $_POST ['motdepasse'] )) {
			if (!empty ( $_POST ['login'] ) && !empty ( $_POST ['motdepasse'] )) {
				
				
				
				
				if (isset($pdo)) {
					$testpse = $_POST ['login'];
					$testmdp = $_POST ['motdepasse'];
					$req = "select count(*) as nb from user where login='".$_POST['login']."' and mdp='".$_POST['motdepasse']."';";
					

					//connexion est  mon identifiant de connexion à ma base de données
					$result = $pdo->connexion->query("select * from user;");
					
					print_r("\nrésultat de la requête".($result->rowCount()));

					
					
					if (!$result) {
						echo 'err req' . "<br>";
						echo '<script>$(document).ready(function(){alert ("erreur requ�te de v�rif connexion !");});</script>';
					} else {
						if ($result->fetch(P
								DO::FETCH_OBJ )->nb != 1) {
							echo 'erreur id/mdp' . "<br>";
							echo '<script>$(document).ready(function(){alert ("erreur identifiant mot de passe!");});</script>' . "<br>";
						} else {
							//cet user existe et je le connecte
							$_SESSION ['connexion'] = true;
							$_SESSION ['login'] = $_POST ['login'];
							setcookie ( 'TP_PHP_Objet', $_SESSION ['login'], time () + 365 * 24 * 3600, null, null, false, true );


							//je vérifie si il est en plus admin
							$req_admin = "select admin from user where login='" . $_POST ['login'] . "' and mdp='" . $_POST ['motdepasse'] . "';";
							$result_admin = $pdo->connexion->query ( $req_admin );
							if ($result_admin->fetch(PDO::FETCH_OBJ)->admin == 1) {

								$_SESSION ['admin'] = true;
							}
						}
					}
				}
			}
		}
	}
}
function deconnecte() {
	if (isset ( $_POST ['deconnexion'] )) {
		$_SESSION = array ();
		session_destroy ();
		header ( 'Location: http://localhost/ppesite/index.php' );
	}
}



?>